package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jyy on 2016/10/19 0019.
 */
@Service
@Transactional(readOnly = true)
public class UserService {

    @Autowired
    private UserDao userDao;

    public User queryUser(String id){
        return userDao.queryUser(id);
    }

    public String queryArea(User user){
        return userDao.queryAear(user);
    }

    // 更改代理商个人信息 我的代理中心
    @Transactional(readOnly = false)
    public void saveUpdateAgent(User user){
        user.setUpdateDate(new Date());
        userDao.saveUpdateAgent(user);
    }
    // 更改代理商状态
    @Transactional(readOnly = false)
    public void updateAgentStatus(String id){
        User userAgent = userDao.queryUser(id);

        if(userAgent.getActiveFlag() == 0){
            userAgent.setActiveFlag(1);
        }else{
            userAgent.setActiveFlag(0);
        };
        userAgent.setUpdateDate(new Date());
        userDao.saveUpdateAgentStatus(userAgent);
    }

    // 代理商列表 jyy
    public Page<User> findAgentByPage(Page<User> userAgentPage, User userAgent) {

//        // 设置默认时间范围，默认当前月
//        if (userAgent.getBeginDate() == null || "".equals(userAgent.getBeginDate())){
//            userAgent.setBeginDate(DateUtils.formatDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1)));
//        }
//        if (userAgent.getEndDate() == null || "".equals(userAgent.getEndDate())){
//            userAgent.setEndDate(DateUtils.formatDate(DateUtils.addMonths(DateUtils.parseDate(userAgent.getBeginDate()), 1)));
//        }

        userAgent.setPage(userAgentPage);
        List<User> list = userDao.findAgentByPage(userAgent);
        userAgentPage.setList(list);
        return userAgentPage;
    }
    // 查询所有代理商区域
    public List<Map<String,String>> selectAreasByUserId(List<String> list){
        return userDao.selectAreasByUserId(list);
    }
}
